

<style lang="scss">
.banner-form {
    .opt {
        margin-top: 15px;
    }

}

.avatar-uploader-icon,
.el-upload-dragger,
.avatar-uploader,
.avatar {
    height: 178px;
    padding: 0 !important;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
    padding: 0 !important;
    line-height: 178px;
}
</style>


<template>
    <h3>发布轮播图</h3>
     <!-- 表单 -->
     <div class="banner-form">
        <div class="opt" style="position: relative;">
            <!-- 上传文件组件 -->
            <el-upload class="avatar-uploader" drag action="#" :on-change="chooseImageFn" :auto-upload="false"
                :show-file-list="false">
                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon">
                    <Plus />
                </el-icon>
            </el-upload>
            <div v-if="imageUrl" style="width: 178px; text-align: center; margin-top: 5px; position: absolute; top: 5px; left: 5px; z-index: 1000;">
                <el-button @click="photoDeleteFn(imageUrl)" type="danger" plain>删除图片</el-button>
            </div>
        </div>

        <div class="opt">
            <el-input v-model="title" style="width: 100%; height: 44px; font-size: 16px" placeholder="广告标题" />
        </div>

        <div class="opt">
            <el-input v-model="href" style="width: 100%; height: 44px; font-size: 16px" placeholder="广告链接" />
        </div>

        <div class="opt">
            <el-button @click="addBannerFn"  type="primary" size="large">发布轮播图</el-button>
        </div>
    </div>

</template>

<script setup lang="ts">
import { ref } from 'vue'
import { photoUpload, photoDelete,  addBanner, removeBanner, getBannerList } from '../../api/request'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { Action } from 'element-plus'
import api from '../../api/index'
const title = ref('')  //广告标题
const href = ref('')   //广告链接
const imageUrl = ref('')
const pic = ref('')

title.value = "广告标题"+(Math.random().toString().slice(2))
href.value = 'https://www.ohmycode.cn/xxxx?id='+(new Date().getTime())
// 选择图标
const chooseImageFn = async (file: any) => {
    if (file) {
        // 创建表单实例
        let fd = new FormData()
        fd.append('photo', file.raw) // 把图片信息传递给服务器 ，服务器存储好图片，再返回一个图片路径
        // 调用上传图片的方法
        await photoUpload(fd)
            .then(
                content => {
                    // console.log("图片路径信息:",content)
                    let { code, picUrl } = content;
                    if (code == 200) {
                        // 赋值（响应式）
                        imageUrl.value = api.url + picUrl;
                        pic.value = picUrl;
                    }
                    else {
                        ElMessage.error(content.msg);
                    }
                }
            )
    }

}
// 删除图片
const photoDeleteFn = async (picUrl: string) => {
    await photoDelete({ picUrl })
        .then(
            content => {
                // console.log(content);
                let { code } = content;
                if (code == 200) {
                    // 重置图片路径
                    imageUrl.value = "";
                }
            }
        )
}

// 添加轮播图
const addBannerFn = async () => {
    let tit = title.value;
    let imgUrl = pic.value;
    let url = href.value;

    if (imgUrl == '' || url == '') {
        ElMessage.error("缺少参数，请检查再提交");
        return false;
    }
    await addBanner({ title: tit, imgUrl, href: url })
        .then(
            content => {
                console.log(content)
                let { code, msg } = content;
                // 添加分类名称成功
                if (code == 200) {
                    ElMessage.success(msg);
                    // 重置表单
                    // title.value = "";
                    // href.value = "";
                    title.value = "广告标题"+(Math.random().toString().slice(2))
                    href.value = 'https://www.ohmycode.cn/xxxx?id'+(new Date().getTime())


                    imageUrl.value = "";
                    pic.value = "";
                }
                else {
                    ElMessage.error(msg)
                }
            }
        )
}


</script>